package ie.dkit.semanticPhotoWeb.client.tools;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.gwt.user.client.ui.Label;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Radio;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.layout.HorizontalLayout;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.panels.photo.RelationPanel;

final public class RelationCreator extends Window {

	private static RelationCreator singelton;

	private ImageDTO m_RelationActivator;

	private ImageDTO m_CurrentImageDTO;

	private ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private FieldSet predefinedFieldSet, userFieldSet;

	private FieldSet m_Friendship, m_Physical, m_Professional, m_Geographical,
			m_Family, m_Romantic;

	private Radio contactFriendship, acquanitanceFriendship, friendFriendship,
			none, coResidentGeographical, neighborGeographical,
			noneGeographical, childFamily, parentFamily, siblingFamily,
			spouseFamily, kinFamily, noneFamily;

	private Checkbox metPhysical, coWorkerProfessional, colleagueProfessional,
			museRomantic, crushRomantic, dateRomantic, sweetheartRomantic;

	private List m_RelationControllerListener;

	private TextArea ownAttributes;

	public static RelationCreator getInstance(
			ClientServerConnectorAsync clientServerConnectorAsync) {
		synchronized (singelton) {
			if (singelton == null) {
				singelton = new RelationCreator(clientServerConnectorAsync);
			}
			return singelton;
		}
	}

	private RelationCreator(
			ClientServerConnectorAsync clientServerConnectorAsync) {
		super("Relation Creator", true, false);
		// Fix due issues..
		setSize(360, 470);
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		m_RelationControllerListener = new ArrayList();
		setCloseAction(Window.HIDE);
	}

	public void setInRelation(ImageDTO imageDTO) {
		m_CurrentImageDTO = imageDTO;
		if (m_RelationActivator == null) {
			MessageBox
					.alert("Please browse to the corresponding Picture and press the \"Set in Relation Button\"");
			m_RelationActivator = imageDTO;
			fireActivation(imageDTO);
		} else {
			createPanel();
			this.show();
		}
	}

	public boolean isRelationMode() {
		return m_RelationActivator != null;
	}

	public void addRelationControllerListener(
			RelationControllerListener listener) {
		if (!m_RelationControllerListener.contains(listener)) {
			m_RelationControllerListener.add(listener);
		}
	}

	public void removeRelationControllerListener(
			RelationControllerListener listener) {
		m_RelationControllerListener.remove(listener);
	}

	private void fireActivation(ImageDTO activator) {
		for (int i = 0; i < m_RelationControllerListener.size(); i++) {
			((RelationControllerListener) m_RelationControllerListener.get(i))
					.relationModeActivated(activator);
		}
	}

	private void fireDeactivation() {
		for (int i = 0; i < m_RelationControllerListener.size(); i++) {
			((RelationControllerListener) m_RelationControllerListener.get(i))
					.relationModeDeactivated();
		}
	}

	private void createPanel() {
		clear();

		// create the form
		FormPanel formPanel = new FormPanel();
		formPanel.setFrame(true);
		formPanel.setPaddings(5, 5, 5, 0);
		formPanel.setWidth(350);
		formPanel.setLabelWidth(75);

		// Create the predefined Relations
		predefinedFieldSet = new FieldSet();
		predefinedFieldSet.setCheckboxToggle(true);
		predefinedFieldSet.setFrame(true);
		predefinedFieldSet.setTitle("Predefined Attributes (XFN)");
		predefinedFieldSet.setCollapsed(false);
		{
			m_Friendship = new FieldSet();
			m_Friendship.setLayout(new HorizontalLayout(4));
			m_Friendship.add(new Label("friendship"));
			contactFriendship = new Radio("contact", "friendship");
			m_Friendship.add(contactFriendship);
			acquanitanceFriendship = new Radio("acquanitance", "friendship");
			m_Friendship.add(acquanitanceFriendship);
			friendFriendship = new Radio("friend", "friendship");
			m_Friendship.add(friendFriendship);
			none = new Radio("none", "friendship");
			m_Friendship.add(none);
			predefinedFieldSet.add(m_Friendship);
		}
		{
			m_Physical = new FieldSet();
			m_Physical.setLayout(new HorizontalLayout(4));
			m_Physical.add(new Label("physical"));
			metPhysical = new Checkbox("met");
			m_Physical.add(metPhysical);
			predefinedFieldSet.add(m_Physical);
		}
		{
			m_Professional = new FieldSet();
			m_Professional.setLayout(new HorizontalLayout(4));
			m_Professional.add(new Label("professional"));
			coWorkerProfessional = new Checkbox("co-worker");
			m_Professional.add(coWorkerProfessional);
			colleagueProfessional = new Checkbox("colleague");
			m_Professional.add(colleagueProfessional);
			predefinedFieldSet.add(m_Professional);
		}
		{
			m_Geographical = new FieldSet();
			m_Geographical.setLayout(new HorizontalLayout(4));
			m_Geographical.add(new Label("geographical"));
			coResidentGeographical = new Radio("co-resident", "geographical");
			m_Geographical.add(coResidentGeographical);
			neighborGeographical = new Radio("neighbor", "geographical");
			m_Geographical.add(neighborGeographical);
			noneGeographical = new Radio("none", "geographical");
			m_Geographical.add(noneGeographical);
			predefinedFieldSet.add(m_Geographical);
		}
		{
			m_Family = new FieldSet();
			m_Family.setLayout(new HorizontalLayout(4));
			m_Family.add(new Label("family"));
			childFamily = new Radio("child", "family");
			m_Family.add(childFamily);
			parentFamily = new Radio("parent", "family");
			m_Family.add(parentFamily);
			siblingFamily = new Radio("sibling", "family");
			m_Family.add(siblingFamily);
			spouseFamily = new Radio("spouse", "family");
			m_Family.add(spouseFamily);
			kinFamily = new Radio("kin", "family");
			m_Family.add(kinFamily);
			noneFamily = new Radio("none", "family");
			m_Family.add(noneFamily);
			predefinedFieldSet.add(m_Family);

		}
		{
			m_Romantic = new FieldSet();
			m_Romantic.setLayout(new HorizontalLayout(4));
			m_Romantic.add(new Label("romantic"));
			museRomantic = new Checkbox("muse");
			m_Romantic.add(museRomantic);
			crushRomantic = new Checkbox("crush");
			m_Romantic.add(crushRomantic);
			dateRomantic = new Checkbox("date");
			m_Romantic.add(dateRomantic);
			sweetheartRomantic = new Checkbox("sweetheart");
			m_Romantic.add(sweetheartRomantic);
			predefinedFieldSet.add(m_Romantic);
		}

		formPanel.add(predefinedFieldSet);

		// create first collapsible fieldset with checkbox
		userFieldSet = new FieldSet();
		userFieldSet.setCheckboxToggle(true);
		userFieldSet.setFrame(true);
		userFieldSet.setTitle("User defined Attributes");
		userFieldSet.setCollapsed(true);

		// add fields to the User FieldSet
		ownAttributes = new TextArea("Own attributes (blank serperated)");
		ownAttributes.setAllowBlank(false);
		userFieldSet.add(ownAttributes);

		formPanel.add(userFieldSet);

		formPanel.addButton(new Button("Set in Relation",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						MessageBox.wait("Set pictures in relation");
						m_ClientServerConnectorAsync.linkToOtherPicture(
								m_RelationActivator, m_CurrentImageDTO,
								getAttributesString(),
								new AsyncCallbackAdapter() {
									public void onSuccess(Object result) {
										MessageBox
												.alert("Pictures succesfully linked");
										m_RelationActivator = null;
										fireDeactivation();
										RelationCreator.this.hide();
									}
								});
					}
				}));
		formPanel.addButton(new Button("Cancel", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				RelationCreator.this.hide();
			}
		}));

		formPanel.addButton(new Button("Abort Relation mode",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						m_RelationActivator = null;
						fireDeactivation();
						RelationCreator.this.hide();
					}
				}));

		add(formPanel);
		doLayout();
	}

	public String getAttributesString() {
		String attributeString = "";
		if (!predefinedFieldSet.isCollapsed()) {

			if (contactFriendship.getValue()) {
				attributeString += "contact ";
			} else if (acquanitanceFriendship.getValue()) {
				attributeString += "acquanitance ";
			} else if (friendFriendship.getValue()) {
				attributeString += "friend ";
			} else if (none.getValue()) {
				// Do Nothing
			}
			if (metPhysical.getValue()) {
				attributeString += "met ";
			}
			if (coWorkerProfessional.getValue()) {
				attributeString += "coWorker ";
			}
			if (colleagueProfessional.getValue()) {
				attributeString += "colleague ";
			}
			if (coResidentGeographical.getValue()) {
				attributeString += "coResident ";
			} else if (neighborGeographical.getValue()) {
				attributeString += "neighbor ";
			} else if (noneGeographical.getValue()) {
				// Do Nothing
			}

			if (childFamily.getValue()) {
				attributeString += "child ";
			} else if (parentFamily.getValue()) {
				attributeString += "parent ";
			} else if (siblingFamily.getValue()) {
				attributeString += "sibling ";
			} else if (spouseFamily.getValue()) {
				attributeString += "spouse ";
			} else if (kinFamily.getValue()) {
				attributeString += "kin ";
			} else if (noneFamily.getValue()) {
				// Do Nothing
			}

			if (museRomantic.getValue()) {
				attributeString += "muse ";
			}
			if (crushRomantic.getValue()) {
				attributeString += "crush ";
			}
			if (dateRomantic.getValue()) {
				attributeString += "date ";
			}
			if (sweetheartRomantic.getValue()) {
				attributeString += "sweetheart ";
			}
		}

		if (!userFieldSet.isCollapsed()) {
			attributeString += ownAttributes.getText();
		}
		return attributeString;
	}
}
